﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SetPotLocaByNet : MonoBehaviour
{
    /// <summary>
    /// 从这里获取源数据
    /// </summary>
    public UnityUWBClient netClient;
    /// <summary>
    /// 导航系统
    /// </summary>
    public GameObject NaMSystem;
    /// <summary>
    /// 模拟场地的ID
    /// </summary>
    public int areaId { get; set; }
    /// <summary>
    /// 锚点显示对象
    /// </summary>
    public Anchor[] anchors;
    /// <summary>
    /// 目标显示对象
    /// </summary>
    public TagLocal[] tags;
    /// <summary>
    /// 结果显示对象
    /// </summary>
    public Result[] results;

    void Awake()
    {
        anchors = NaMSystem.GetComponentsInChildren<Anchor>();
        tags = NaMSystem.GetComponentsInChildren<TagLocal>();
        results = NaMSystem.GetComponentsInChildren<Result>();
        if (anchors.Length < 3)
        {
            Debug.LogError("锚点数量至少3个！");
        }

        if (tags.Length < 1)
        {
            Debug.LogError("目标点数量至少1个！");
        }

        for (int i = 0; i < 3; i++)
        {
            if (anchors[i] != null)
            {
                anchors[i].area = this.areaId;
                anchors[i].id = i;
                Debug.Log("锚点：" + anchors[i].name);
            }
        }

        if (tags[0] != null)
        {
            tags[0].id = 0;
            Debug.Log("目标点：" + tags[0].name);
        }

        if (results[0] != null)
        {
            results[0].id = 0;
            Debug.Log("解：" + results[0].name);
        }
    }

    void Start()
    {
        
    }

    void Update()
    {
        if (netClient.isLogin)
        {
            Vec3dInfo tag, anc1, anc2, anc3;

            tag = new Vec3dInfo();
            tag.x = netClient.tagX;
            tag.y = netClient.tagY;
            tag.z = netClient.tagZ;
            anc1 = new Vec3dInfo();
            anc1.x = netClient.anchor0X;
            anc1.y = netClient.anchor0Y;
            anc1.z = netClient.anchor0Z;
            anc2 = new Vec3dInfo();
            anc2.x = netClient.anchor1X;
            anc2.y = netClient.anchor1Y;
            anc2.z = netClient.anchor1Z;
            anc3 = new Vec3dInfo();
            anc3.x = netClient.anchor2X;
            anc3.y = netClient.anchor2Y;
            anc3.z = netClient.anchor2Z;

            //设置目标点坐标
            tags[0].SetTag(tag);
            //设置锚点坐标
            anchors[0].SetAnchor(anc1, 0);
            anchors[1].SetAnchor(anc2, 0);
            anchors[2].SetAnchor(anc3, 0);
        }
    }
}
